from datetime import datetime
from RealProject import db
from sqlalchemy.dialects.mysql import LONGTEXT
from enum import IntEnum


class BaseModel(db.Model):
    # 基类模型
    __abstract__ = True

    add_date = db.Column(db.DateTime, nullable=False, default=datetime.now)  # 创建时间
    pub_date = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=False)  # 更新时间


class PostPublishType(IntEnum):
    # 文章发布类型
    draft = 1  # 草稿
    show = 2  # 发布


class Category(BaseModel):
    # 文章分类模型
    cate_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    icon = db.Column(db.String(128), nullable=True)
    post = db.relationship('Post', back_populates='category', cascade="all,delete", passive_deletes=True)
    friend_post = db.relationship('Friend_post', back_populates='category', cascade="all,delete", passive_deletes=True)
    events_post = db.relationship('Events_post', back_populates='category', cascade="all,delete", passive_deletes=True)

    # post = db.relationship('Post',backref='category',lazy=True)

    def __repr__(self):
        return '<Category %r>' % self.name


class Post(BaseModel):
    # 文章
    post_id = db.Column(db.Integer, primary_key=True)  # 文章id
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id', ondelete="CASCADE"), nullable=False)
    title = db.Column(db.String(128), nullable=False)  # 文章标题
    desc = db.Column(db.String(200), nullable=True)  # 文章描述
    content = db.Column(LONGTEXT, nullable=True)
    has_type = db.Column(db.Enum(PostPublishType), server_default='show', nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey('category.cate_id', ondelete="CASCADE"), nullable=False)  # 文章分类id

    category = db.relationship("Category", back_populates="post")
    user = db.relationship('User', back_populates="post")

    def __repr__(self):
        return '<Post %r>' % self.title


class Friend_post(BaseModel):
    # 朋友文章
    post_id = db.Column(db.Integer, primary_key=True)  # 文章id
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id', ondelete="CASCADE"), nullable=False)
    title = db.Column(db.String(128), nullable=False)  # 文章标题
    desc = db.Column(db.String(200), nullable=True)  # 文章描述
    content = db.Column(LONGTEXT, nullable=True)
    has_type = db.Column(db.Enum(PostPublishType), server_default='show', nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey('category.cate_id', ondelete="CASCADE"), nullable=False)  # 文章分类id
    new_old = db.Column(db.Integer, nullable=False)

    category = db.relationship("Category", back_populates="friend_post")
    user = db.relationship('User', back_populates="friend_post")

    def __repr__(self):
        return '<Friend_post %r>' % self.title


class Events_post(BaseModel):
    # 朋友文章
    post_id = db.Column(db.Integer, primary_key=True)  # 文章id
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id', ondelete="CASCADE"), nullable=False)
    title = db.Column(db.String(128), nullable=False)  # 文章标题
    desc = db.Column(db.String(200), nullable=True)  # 文章描述
    content = db.Column(LONGTEXT, nullable=True)
    has_type = db.Column(db.Enum(PostPublishType), server_default='show', nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey('category.cate_id', ondelete="CASCADE"), nullable=False)  # 文章分类id

    category = db.relationship("Category", back_populates="events_post")
    user = db.relationship('User', back_populates="events_post")

    def __repr__(self):
        return '<Events_post %r>' % self.title


# class FamilyMember(BaseModel):
#     # 家族成员模型
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(128), nullable=False)  # 成员姓名
#     relation = db.Column(db.String(128))  # 与中心人物的关系
#     parent_id = db.Column(db.Integer, db.ForeignKey('family_member.id'))  # 父成员的ID
#     image_path = db.Column(db.String(255))  # 图片路径
#
#     # 定义子成员的关系
#     children = db.relationship('FamilyMember', backref=db.backref('parent', remote_side=[id]), passive_deletes=True)
#
#     def __repr__(self):
#         return '<FamilyMember %r>' % self.name


